$(document).ready(function(){
	//Set class current for page
	page_current = $('.page').val();
	$('#'+page_current).addClass('current');
	
	//Delete image, render, panorama, animation
	$('.delete').click(function(){
		rs = confirm_();
		var id = $(this).attr('id');
		var url_ajax = '';
		
		if( $(this).attr('page') !== undefined ){
			page_current = $(this).attr('page');
		}
			
		if(page_current == 'tag')
			url_ajax = base_url + '/admin/ajax_delete_tag';
		else if(page_current == 'protfolio')
			url_ajax = base_url + '/admin/ajax_delete_protfolio';	
		else if(page_current == 'image')
			url_ajax = base_url + '/admin/ajax_delete_image';
		else if(page_current == 'video')
			url_ajax = base_url + '/admin/ajax_delete_video';	
		
		if(rs && url_ajax){
			//Call ajax
			$.ajax({
				type:'POST',
				data:'id='+id,
				dataType:'json',
				url: url_ajax,
				beforeSend:function(){
				},
				success:function(msg){
					if(msg.status == 'error'){
						alert(msg.msg);
					}
					else if(msg.status == 'ok')
					{
						$('#product_'+id).remove();
						$('#row_'+id).remove();
						$('#image_'+id).remove();
					}
				}
			});				
		}
	});
	
	//Upload image in image_edit page
	$('#ajax_upload_file').change(function(){
		$('#form').submit();
	});
	
	//Add new image
	image_count = 1;
	$('#add_image').click(function(){
		$('#input_file').append('<input type="file" name="newfile'+image_count+'" class="upload_file">');
		image_count++;
		$('#num_file').val(image_count);
	});
	
	//Submit add landmark
	$('form').submit(function(e){
		check_ = 0;
	    $('input[rel=required]').each(function(){
            if($.trim($(this).val()) == ''){
                var name = $(this).attr('name');
                alert(name + ' is required');
                $(this).focus();
                check_ = 1;
            	e.preventDefault();
            	return false;                
            }
	    });

		if(check_ == 0){
			$('select[rel=required]').each(function(){
				if( $(this).val() == 0 ){
					alert($(this).attr('name') + ' is required');
					$(this).focus();
					e.preventDefault();
					return false;						
				}
			});					
		}
	});	
	
	//Editor set-up
	if( $('#wysiwyg').length>0 )
		$('#wysiwyg').wysiwyg();	
});

function confirm_(){
	if( confirm('Are you sure to delete') )
		return true;
	return false;
}